﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	Codici2
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Map_Fvg
{
    public partial class Codici2 : Form
    {
        public Codici2()
        {
            InitializeComponent();

            for (int i = 0; i < ViewQ.nSimbA; ++i)
            {
                    string codice = ViewQ.vectA[i].codice;
                    if (codice.Contains("000")) continue;
                    string codice1 = codice.Substring(0, 2);
                    codice1 = codice1 + "000";
                    codice1 = codice1 + codice.Substring(5, 2);
                    ViewQ.vectA[i].codice = codice1;
                    Save.modificaArea = 2;
                    }

                for (int i = 0; i < ViewQ.nSimb; ++i)
                {
                    string codice = ViewQ.vectS[i].codice;
                    if (codice.Contains("000")) continue;
                    string codice1 = codice.Substring(0, 2);
                    codice1 = codice1 + "000";
                    codice1 = codice1 + codice.Substring(5, 2);
                    ViewQ.vectS[i].codice = codice1;
                    Save.modificaLinea = 2;
                }
                for (int i = 0; i < ViewQ.nSimbT; ++i)

                {
                    string codice = ViewQ.vectT[i].codice;
                    if (codice.Contains("000")) continue;
                    string codice1 = codice.Substring(0, 2);
                    codice1 = codice1 + "000";
                    codice1 = codice1 + codice.Substring(5, 2);
                    ViewQ.vectT[i].codice = codice1;
                    Save.modificaTesto = 2;
                }

                for (int i = 0; i < ViewQ.nSimbP; ++i)
                {
                    string codice = ViewQ.vectP[i].codice;
                    if (codice.Contains("000")) continue;
                    string codice1 = codice.Substring(0, 2);
                    codice1 = codice1 + "000";
                    codice1 = codice1 + codice.Substring(5, 2);
                    ViewQ.vectP[i].codice = codice1;
                    Save.modificaPunto = 2;
                }

                eliminaduplicati();

                AggiornaSimboli agSim = new AggiornaSimboli();
                agSim.aggiornaSimboli1();

                ViewQ.ActiveForm.Refresh();

        }




        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }

        private void aggiornaSimboli()
        {
            for (int i = 0; i < ViewQ.nSimb; ++i)
            {
                if (ViewQ.vectS[i].selected == 0) continue;
                string ss = ViewQ.vectS[i].codice.ToString();

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from linea where codice_stclasse = '" + ss + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                ViewQ.vectS[i].penType = int.Parse(reader.GetString(3).ToString());
                                ViewQ.vectS[i].tratto = reader.GetString(4).ToString();

                                string[] words;
                                char[] delimiterChars = { ' ', ',' };

                                string s1 = reader.GetString(5).ToString();
                                words = s1.Split(delimiterChars);
                                byte b1 = byte.Parse(words[0]);
                                byte b2 = byte.Parse(words[1]);
                                byte b3 = byte.Parse(words[2]);
                                byte b4 = byte.Parse(words[3]);
                                ViewQ.vectS[i].icolor = Color.FromArgb(b1, b2, b3, b4);
                            }
                        }
                    }
                    conn.Close();
                }

            }

            for (int i = 0; i < ViewQ.nSimbT; ++i)
            {
                if (ViewQ.vectT[i].selected == 0) continue;
                string ss = ViewQ.vectT[i].codice.ToString();

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from testo where codice_testo = '" + ss + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                string s1 = reader.GetString(2).ToString();
                                ViewQ.vectT[i].iFont = stringToFont(s1);

                                ViewQ.vectT[i].altezza = float.Parse(reader.GetString(4).ToString());

                                string[] words;
                                char[] delimiterChars = { ' ', ',' };

                                s1 = reader.GetString(3).ToString();
                                words = s1.Split(delimiterChars);
                                byte b1 = byte.Parse(words[0]);
                                byte b2 = byte.Parse(words[1]);
                                byte b3 = byte.Parse(words[2]);
                                byte b4 = byte.Parse(words[3]);
                                ViewQ.vectT[i].icolor = Color.FromArgb(b1, b2, b3, b4);
                            }
                        }
                        conn.Close();
                    }
                }
            }

            for (int i = 0; i < ViewQ.nSimbA; ++i)
            {
                if (ViewQ.vectA[i].selected == 0) continue;
                string ss = ViewQ.vectA[i].codice.ToString();

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from area where codice_area = '" + ss + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                string s1 = reader.GetString(2).ToString();
                                ViewQ.vectA[i].codice_bordo = int.Parse(reader.GetString(4).ToString());
                                ViewQ.vectA[i].bordo = reader.GetString(5).ToString();
                                ViewQ.vectA[i].codice_camp = int.Parse(reader.GetString(2).ToString());
                                ViewQ.vectA[i].campitura = reader.GetString(3).ToString();


                                s1 = reader.GetString(6).ToString();
                                string[] words;
                                char[] delimiterChars = { ' ', ',' };

                                words = s1.Split(delimiterChars);
                                byte b1 = byte.Parse(words[0]);
                                byte b2 = byte.Parse(words[1]);
                                byte b3 = byte.Parse(words[2]);
                                byte b4 = byte.Parse(words[3]);
                                ViewQ.vectA[i].icolor = Color.FromArgb(b1, b2, b3, b4);
                            }
                        }
                        conn.Close();
                    }
                }
            }

            for (int i = 0; i < ViewQ.nSimbP; ++i)
            {
                //           if (ViewQ.vectP[i].selected == 0) continue;
                string ss = ViewQ.vectP[i].codice.ToString();

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from point where codice_point = '" + ss + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                ViewQ.vectP[i].simbolo = int.Parse(reader.GetString(2).ToString());

                                ViewQ.vectP[i].paramSimb = reader.GetString(3).ToString();

                                string s1 = reader.GetString(4).ToString();
                                s1 = s1.Trim();
                                if (s1.Length > 0) ViewQ.vectP[i].fileName = s1;

                                ViewQ.vectP[i].dimensione = float.Parse(reader.GetString(5).ToString());
                                s1 = reader.GetString(6).ToString();
                            }
                        }
                        conn.Close();
                    }
                }
            }
        }

        private string aggiungiApostrofo(string orig)
        {
            string s1 = "";
            string s2 = ""; string s3 = "";
            int ipos = 0, len = 0;
            int ipos1 = 0;
            s2 = orig;

        L10: ipos = s2.IndexOf('\x27', ipos1);

            if (ipos > 0)
            {
                len = s2.Length;
                s1 = s2.Substring(0, ipos);
                s1 = string.Concat(s1, "\x27\x27");
                s3 = s2.Substring(ipos + 1, len - ipos - 1);
                s1 = string.Concat(s1, s3);
                s2 = s1;
                ipos1 = ipos + 2;
                goto L10;
            }

            return (s2);
        }

        private Font stringToFont(string ss)
        {
            float sizef = 1.0F;
            int ipos = ss.IndexOf("Size=", 0);
            if (ipos > 0)
            {
                int ipos1 = ss.IndexOf(",", ipos);
                int len = ipos1 - ipos - 5;
                string s1 = ss.Substring(ipos + 5, len);
                sizef = float.Parse(s1);
                //              MessageBox.Show(s1 + "  " + sizef.ToString()); 
            }

            Font ifont = new Font(ss, sizef);
            return (ifont);
        }

        private struct SimbL
        {
            public string codice;
 //           public int ipos;
        }

        private struct SimbT
        {
            public string codice;
 //           public int ipos;
        }

        private struct SimbP
        {
            public string codice;
  //          public int ipos;
        }

        private struct SimbA
        {
            public string codice;
  //          public int ipos;
        }

        private void eliminaduplicati()
        {
            string mess = String.Empty;
            int tot = 0;

            int irept = 0;
            int ipt = 1;
            int nSimb = ViewQ.nSimb;
            SimbL[] vectSS = new SimbL[nSimb];
            vectSS[0].codice = ViewQ.vectS[0].codice;
            for(int i = 1;i < nSimb ;++i)
            {
                string codice = ViewQ.vectS[i].codice;
                bool dupl = false;
                for (int j = 0; j < ipt; ++j)
                {
                    string codice1 = vectSS[j].codice;
                    if (codice1.Contains(codice))
                    {
                        dupl = true;
                        ++irept;
                    }

                }
                if (!dupl)
                {
                    vectSS[ipt].codice = ViewQ.vectS[i].codice;
                    ++ipt;
                }

            }
            if (irept > 0)
            {
                Save.modificaLinea = 2;
                mess = irept.ToString() + "  ripetizioni linea " + nSimb.ToString() + "\n";
                tot = irept;
            }

            irept = 0;
            ipt = 1;
            nSimb = ViewQ.nSimbT;
            SimbT[] vectTT = new SimbT[nSimb];
            vectTT[0].codice = ViewQ.vectT[0].codice;
            for (int i = 1; i < nSimb; ++i)
            {
                string codice = ViewQ.vectT[i].codice;
                bool dupl = false;
                for (int j = 0; j < ipt; ++j)
                {
                    string codice1 = vectTT[j].codice;
                    if (codice1.Contains(codice))
                    {
                        dupl = true;
                        ++irept;
                    }

                }
                if (!dupl)
                {
                    vectTT[ipt].codice = ViewQ.vectT[i].codice;
                    ++ipt;
                }

            }
            if (irept > 0)
            {
                Save.modificaTesto = 2;
                mess += irept.ToString() + "  ripetizioni testo " + nSimb.ToString() + "\n";
                tot += irept;
            }


            irept = 0;
            ipt = 1;
            nSimb = ViewQ.nSimbP;
            SimbP[] vectPP = new SimbP[nSimb];
            vectPP[0].codice = ViewQ.vectP[0].codice;
            for (int i = 1; i < nSimb; ++i)
            {
                string codice = ViewQ.vectP[i].codice;
                bool dupl = false;
                for (int j = 0; j < ipt; ++j)
                {
                    string codice1 = vectPP[j].codice;
                    if (codice1.Contains(codice))
                    {
                        dupl = true;
                        ++irept;
                    }

                }
                if (!dupl)
                {
                    vectPP[ipt].codice = ViewQ.vectP[i].codice;
                    ++ipt;
                }

            }
            if (irept > 0)
            {
                Save.modificaPunto = 2;
                mess += irept.ToString() + "  ripetizioni punto " + nSimb.ToString() + "\n";
                tot += irept;
            }


            irept = 0;
            ipt = 1;
            nSimb = ViewQ.nSimbA;
            SimbA[] vectAA = new SimbA[nSimb];
            vectAA[0].codice = ViewQ.vectA[0].codice;
            for (int i = 1; i < nSimb; ++i)
            {
                string codice = ViewQ.vectA[i].codice;
                bool dupl = false;
                for (int j = 0; j < ipt; ++j)
                {
                    string codice1 = vectAA[j].codice;
                    if (codice1.Contains(codice))
                    {
                        dupl = true;
                        ++irept;
                    }

                }
                if (!dupl)
                {
                    vectAA[ipt].codice = ViewQ.vectA[i].codice;
                    ++ipt;
                }

            }
            if (irept > 0)
            {
                Save.modificaArea = 2;
                mess += irept.ToString() + "  ripetizioni area " + nSimb.ToString() + "\n";
                tot += irept;

            }
            if(tot > 0) MessageBox.Show(mess);

        }

    }
}
